﻿using Newtonsoft.Json.Linq;
using SmartWeighMonitor.Utils;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace SmartWeigh
{
    /// <summary>
    /// LoginWin.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWin : Window
    {
        public LoginWin()
        {
            InitializeComponent();
            this.Loaded += Win_loaded;
        }
        private void Win_loaded(object sender, RoutedEventArgs e)
        {
            string saveUsernameAndPassword = ConfigurationManager.AppSettings["saveUsernameAndPassword"];
            if (null != saveUsernameAndPassword)
            {
                txtUsername.Text = ConfigurationManager.AppSettings["savingUsername"];
                txtPassword.Password = ConfigurationManager.AppSettings["savingPassword"];
                cbSavePassword.IsChecked = true;
            }
            else
            {
                cbSavePassword.IsChecked = false;
            }
        }
        private void Button_Click_Login(object sender, RoutedEventArgs e)
        {
            string username = txtUsername.Text;
            string password = txtPassword.Password;

            if ("".Equals(username))
            {
                MessageBox.Show("请输入用户名!");
                return;
            }
            if ("".Equals(password))
            {
                MessageBox.Show("请输入密码!");
                return;
            }
            JObject paramJSON = new JObject();
            paramJSON.Add("account", username);
            paramJSON.Add("password", password);

            //MessageBox.Show("http://" + App.getConfig().HttpServerIp + ":" + App.getConfig().AuthHttpServerPort + "/auth/accessCodeWpf");

            String response = HttpPostHelper.doHttpPost("http://" + App.GetConfig().HttpServerIp + ":" + App.GetConfig().AuthHttpServerPort + "/guns/loginApi", paramJSON.ToString());
            JObject respJson = JObject.Parse(response);
            string status = respJson["success"].ToString();
            if (status.ToLower() != "true")
            {
                MessageBox.Show("登录失败");
                return;
            }
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if ((bool)cbSavePassword.IsChecked)
            {
                string saveUsernameAndPassword = null;
                if (config.AppSettings.Settings["saveUsernameAndPassword"] != null)
                {
                    saveUsernameAndPassword = config.AppSettings.Settings["saveUsernameAndPassword"].Value;
                }
                if (null == saveUsernameAndPassword)
                {
                    config.AppSettings.Settings.Add("saveUsernameAndPassword", "1");
                    config.AppSettings.Settings.Add("savingUsername", username);
                    config.AppSettings.Settings.Add("savingPassword", password);
                }
                else
                {
                    config.AppSettings.Settings["savingUsername"].Value = username;
                    config.AppSettings.Settings["savingPassword"].Value = password;
                }
            }
            else
            {
                config.AppSettings.Settings.Remove("saveUsernameAndPassword");
                config.AppSettings.Settings.Remove("savingUsername");
                config.AppSettings.Settings.Remove("savingPassword");
            }
            config.Save(ConfigurationSaveMode.Modified);


            JObject data = (JObject)respJson["data"];
            //App.getGlobalVariables().LoginCorpID = data["companyCode"].ToString();
            //App.getGlobalVariables().LoginUserID = data["userId"].ToString();
            //App.getGlobalVariables().LoginUserCode = data["userCode"].ToString();
            //App.getGlobalVariables().LoginUserName = data["name"].ToString();
            //App.getGlobalVariables().LoginUserDeptCode = data["deptCode"].ToString();
            //App.getGlobalVariables().LoginUserDeptName = data["deptName"].ToString();
            App.getGlobalVariables().LoginUserName=data["loginUser"]["simpleUserInfo"]["realName"].ToString();
            App.getGlobalVariables().Authorization = data["token"].ToString();

            MainWindow mainWindow = new MainWindow();
            mainWindow.Show();
            this.Close();

            /*
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if ((bool)cbSavePassword.IsChecked)
            {
                string username = txtUsername.Text;
                string password = txtPassword.Password;

                string saveUsernameAndPassword = null;
                if (config.AppSettings.Settings["saveUsernameAndPassword"] != null)
                {
                    saveUsernameAndPassword = config.AppSettings.Settings["saveUsernameAndPassword"].Value;
                }
                if (null == saveUsernameAndPassword)
                {
                    config.AppSettings.Settings.Add("saveUsernameAndPassword", "1");
                    config.AppSettings.Settings.Add("savingUsername", username);
                    config.AppSettings.Settings.Add("savingPassword", password);
                }
                else
                {
                    config.AppSettings.Settings["savingUsername"].Value = username;
                    config.AppSettings.Settings["savingPassword"].Value = password;
                }
            }
            else
            {
                config.AppSettings.Settings.Remove("saveUsernameAndPassword");
                config.AppSettings.Settings.Remove("savingUsername");
                config.AppSettings.Settings.Remove("savingPassword");
            }
            config.Save(ConfigurationSaveMode.Modified);

            App.getGlobalVariables().LoginUserID = "001";
            App.getGlobalVariables().LoginUserName = "李四";

            MainWindow mainWindow = new MainWindow();
            mainWindow.Show();

            this.Close();
            */
        }
    }
}
